package com.zjw.touchscreen.listener;

import android.os.Build;
import android.text.InputFilter;
import android.text.Spanned;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import androidx.annotation.RequiresApi;
import com.zjw.touchscreen.bluetooth.KeyboardHelper;
import org.apache.commons.lang3.StringUtils;

@RequiresApi(api = Build.VERSION_CODES.P)
public class KeyboardInputListener implements InputFilter, View.OnKeyListener {

    private String source;

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        if ("\n".equals(source)) {
            KeyboardHelper.sendKeyDown(KeyboardHelper.Modifier.NONE, KeyboardHelper.Key.ENTER);
            KeyboardHelper.sendKeyUp();
            return "";
        }
        if ("".equals(source)) {
            KeyboardHelper.sendKeyDown(KeyboardHelper.Modifier.NONE, KeyboardHelper.Key.BACKSPACE);
            KeyboardHelper.sendKeyUp();
        }else if (isDel(this.source, source.toString())) {
            KeyboardHelper.sendKeyDown(KeyboardHelper.Modifier.NONE, KeyboardHelper.Key.BACKSPACE);
            KeyboardHelper.sendKeyUp();
        }else {
            String sendText = getSendText(this.source, source.toString());
            if (sendText != null && !"".equals(sendText)) {
                for (char c : sendText.toCharArray()) {
                    if (KeyboardHelper.keyMap.containsKey(c)) {// Small case letter
                        KeyboardHelper.sendKeyDown(KeyboardHelper.Modifier.NONE, KeyboardHelper.getKey(c));
                        KeyboardHelper.sendKeyUp();
                    } else if (KeyboardHelper.shiftKeyMap.containsKey(c)) {// Upper case letter
                        KeyboardHelper.sendKeyDown(KeyboardHelper.Modifier.KEY_MOD_LSHIFT, KeyboardHelper.getShiftKey(c));
                        KeyboardHelper.sendKeyUp();
                    }
                }
            }
        }
        this.source = source.toString();
        return "";
    }

    private boolean isDel(String oldSource, String newSource) {
        if (oldSource == null || "".equals(oldSource)) {
            return false;
        }
        return oldSource.startsWith(newSource) && oldSource.length() == newSource.length() + 1;
    }

    private String getSendText(String oldSource, String newSource) {
        if (newSource.length() == 1) {
            return newSource;
        }
        if (StringUtils.isBlank(oldSource)) {
            return newSource;
        }
        if (newSource.startsWith(oldSource) && newSource.length() > oldSource.length()) {
            return newSource.substring(oldSource.length());
        }
        if (!oldSource.startsWith(newSource) && !newSource.startsWith(oldSource)) {
            return newSource;
        }
        return "";
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        switch (event.getAction()) {
            case KeyEvent.ACTION_UP:
                if (keyCode == KeyEvent.KEYCODE_DEL) {
                    KeyboardHelper.sendKeyDown(KeyboardHelper.Modifier.NONE, KeyboardHelper.Key.BACKSPACE);
                    KeyboardHelper.sendKeyUp();
                }
                break;
        }
        return false;
    }
}
